#include "dianzitushu.h"
//µç×ÓÍ¼Êé½çÃæ ÈËÎï½öÖ´ÐÐÒ»´Î
#include "GUI.h"
#include "DIALOG.h"
#include "BUTTON.h"
#include "led.h"
#include "bitmapdisplay.h"
#include <os.h>
#include "rtc.h"
#include "EmWinHZFont.h"
#include "xbffontcreate.h"
#include "xbffontdemo.h"
#include "DIALOG.h"
#include "ff.h"
#include "usart.h"	
#include "malloc.h"
#include <stm32f4xx.h>	   
#include "fattester.h"	 
/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_FRAMEWIN1_0     (GUI_ID_USER + 0x00)
#define ID_LISTBOX1_0     (GUI_ID_USER + 0x01)
#define ID_BUTTON1_0     (GUI_ID_USER + 0x02)
#define ID_BUTTON1_1     (GUI_ID_USER + 0x03)

extern FILINFO fileinfo;
extern DIR dir;
// USER START (Optionally insert additional defines)
// USER END

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

// USER START (Optionally insert additional static data)
// USER END

/*********************************************************************
*
*       _aDialogCreate
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate1[] = {
  { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN1_0, 0, 0, 240, 320, 0, 0x64, 0 },
  { LISTBOX_CreateIndirect, "Listbox", ID_LISTBOX1_0, 0, 0, 231, 249, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "OK", ID_BUTTON1_0, 12, 259, 80, 20, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "BACK", ID_BUTTON1_1, 128, 259, 80, 20, 0, 0x0, 0 },
  // USER START (Optionally insert additional widgets)
  // USER END
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

// USER START (Optionally insert additional static code)
// USER END

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialog1(WM_MESSAGE * pMsg) {
  WM_HWIN hItem;
  int     NCode;
  int     Id;
	FRESULT res;	  
    char *fn; 
		int count=0;
  // USER START (Optionally insert additional variables)
  // USER END

  switch (pMsg->MsgId) {
  case WM_INIT_DIALOG:
    //
    // Initialization of 'Framewin'
    //
    hItem = pMsg->hWin;
    FRAMEWIN_SetText(hItem, "BOOK");
    FRAMEWIN_SetTitleHeight(hItem, 20);
    FRAMEWIN_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
    hItem = WM_GetDialogItem(pMsg->hWin, ID_LISTBOX1_0);
	 
	
	  /* This function is assuming non-Unicode cfg. */
 	fileinfo.lfsize = _MAX_LFN * 2 + 1;
	fileinfo.lfname = mymalloc(SRAMIN,fileinfo.lfsize);	  
	 res = f_opendir(&dir,"0:/book"); //´ò¿ªÒ»¸öÄ¿Â¼
    if (res == FR_OK) 
	{	
		while(1)
		{
	        res = f_readdir(&dir, &fileinfo);                 
	        if (res != FR_OK || fileinfo.fname[0] == 0) break; 
        	fn = *fileinfo.lfname ? fileinfo.lfname : fileinfo.fname;     
	    //   fn ="数学";			/* It is a file. */
			    count++;     
          LISTBOX_AddString(hItem, fn);		
//				printf("1:%s\r\n",  fileinfo.lfname);
//			printf("2:%s\r\n",  fileinfo.fname);
//			printf("3:%s\r\n",  fn);
		} 
  }	  
	LISTBOX_SetFont(hItem,&XBF12_Font);
	myfree(SRAMIN,fileinfo.lfname);  

	SCROLLBAR_CreateAttached(hItem,0xfe);

    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch(Id) {
    case ID_LISTBOX1_0: // Notifications sent by 'Listbox'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        // USER START (Optionally insert code for reacting on notification message)
        // USER END
        break;
      case WM_NOTIFICATION_RELEASED:
        // USER START (Optionally insert code for reacting on notification message)
        // USER END
        break;
      case WM_NOTIFICATION_SEL_CHANGED:
        // USER START (Optionally insert code for reacting on notification message)
        // USER END
        break;
      // USER START (Optionally insert additional code for further notification handling)
      // USER END
      }
      break;
    case ID_BUTTON1_0: // Notifications sent by 'OK'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        // USER START (Optionally insert code for reacting on notification message)
        // USER END
        break;
      case WM_NOTIFICATION_RELEASED:
        // USER START (Optionally insert code for reacting on notification message)
        // USER END
        break;
      // USER START (Optionally insert additional code for further notification handling)
      // USER END
      }
      break;
    case ID_BUTTON1_1: // Notifications sent by 'BACK'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        // USER START (Optionally insert code for reacting on notification message)
        // USER END
        break;
      case WM_NOTIFICATION_RELEASED:
                 	Buttonbmp_Demo(); 
        break;
      // USER START (Optionally insert additional code for further notification handling)
      // USER END
      }
      break;
    // USER START (Optionally insert additional code for further Ids)
    // USER END
    }
    break;
  // USER START (Optionally insert additional message handling)
  // USER END
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}
WM_HWIN dianzitushu(void) 
{
	WM_HWIN hWin;
//buttonbmp_tab[0]=bmBUTTONOFF;
//buttonbmp_tab[1]=bmBUTTONON;
	hWin = GUI_CreateDialogBox(_aDialogCreate1, GUI_COUNTOF(_aDialogCreate1), _cbDialog1, WM_HBKWIN, 0, 0);
	return hWin;
}

void dianzitushu_Demo(void)
{
	
	WM_HWIN hWin;
	
	
	BUTTON_SetDefaultSkin(BUTTON_SKIN_FLEX); 
	CHECKBOX_SetDefaultSkin(CHECKBOX_SKIN_FLEX);
	DROPDOWN_SetDefaultSkin(DROPDOWN_SKIN_FLEX);
	FRAMEWIN_SetDefaultSkin(FRAMEWIN_SKIN_FLEX);
	HEADER_SetDefaultSkin(HEADER_SKIN_FLEX);
	MENU_SetDefaultSkin(MENU_SKIN_FLEX);
	MULTIPAGE_SetDefaultSkin(MULTIPAGE_SKIN_FLEX);
	PROGBAR_SetDefaultSkin(PROGBAR_SKIN_FLEX);
	RADIO_SetDefaultSkin(RADIO_SKIN_FLEX);
	SCROLLBAR_SetDefaultSkin(SCROLLBAR_SKIN_FLEX);
	SLIDER_SetDefaultSkin(SLIDER_SKIN_FLEX);
	SPINBOX_SetDefaultSkin(SPINBOX_SKIN_FLEX); 
	// mf_scan_files("0:");
	hWin=dianzitushu();
	while(1)
	{
	

		GUI_Delay(100);
	}
}

